//
//  S1mpl3AlbumViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "S1mpl3AlbumViewController.h"
#import "TwoToOne.h"
#import "FitHeader.h"
#import "MJRefresh.h"
#import "TabBarController.h"
#import "S1mpl3AlbumTableViewCell.h"
#import "S1mpl3TableViewHeadView.h"
#import "S1mpl3HeadView.h"
#import "UIImage+GaussianBlur.h"

@interface S1mpl3AlbumViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,strong)NSString *totalString;
@property(nonatomic,strong)NSMutableDictionary *albumDic;
@property(nonatomic,strong)S1mpl3HeadView *s1mpl3HeadView;
@property(nonatomic,strong)UIImageView *imageView;
@end

@implementation S1mpl3AlbumViewController

-(void)getData{
    [TwoToOne getDataByString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/%ld/20?albumId=%@&isAsc=true&device=android&pageSize=20", _albumId, _page, _albumId] BodyString:nil WithDataBlock:^(id data) {
//        NSLog(@"%@", [[data objectForKey:@"tracks"] objectForKey:@"list"]);
        NSLog(@"%@", [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/%ld/20?albumId=%@&isAsc=true&device=android&pageSize=20", _albumId, _page, _albumId]);
//        _albumDic = [data objectForKey:@"album"];
        _s1mpl3HeadView.dataSourceDic = [data objectForKey:@"album"];
        _imageView.image = [UIImage imageWithBlurImage:_s1mpl3HeadView.backImageView.image intputRadius:30];
        _totalString = [[data objectForKey:@"album"] objectForKey:@"tracks"];
        if ([[[data objectForKey:@"tracks"] objectForKey:@"list"]count] == 0) {
            [_tableView.footer endRefreshing];
            _tableView.footer.state = MJRefreshStateNoMoreData;
            return ;
        }
        for (NSDictionary *dic in [[data objectForKey:@"tracks"] objectForKey:@"list"]) {
            [_dataSourceArray addObject:dic];
        }
        [_tableView reloadData];
//        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [[TabBarController shareInstance] remainButtonOfPlay:YES];
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]];
//    self.navigationController.navigationBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[TabBarController shareInstance] remainButtonOfPlay:NO];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName]];
}



-(void)loadView{
    [super loadView];
    _page = 1;
    self.dataSourceArray = [NSMutableArray array];
//    self.albumDic = [NSMutableDictionary dictionary];
    
    [self getData];
    _s1mpl3HeadView = [[S1mpl3HeadView alloc] initWithFrame:CGRectMake(0, 0 * FITHeight, self.view.frame.size.width, 250 * FITHeight)];
    _imageView = [[UIImageView alloc] initWithFrame:_s1mpl3HeadView.backImageView.frame];
    _imageView.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _s1mpl3HeadView;
    [self.view addSubview:_tableView];
    
    
//    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [_dataSourceArray removeAllObjects];
//        _page = 1;
//        [self getData];
//    }];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getData];
    }];

}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"S1mpl3透明图"] forBarMetrics:UIBarMetricsDefault];
    self.title = _myTitle;
    // Do any additional setup after loading the view.
}

#pragma mark--scrollView代理方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
    if (scrollView.contentOffset.y > 185 * FITHeight) {
        _imageView.frame = CGRectMake(0, -280 * FITWidth, _s1mpl3HeadView.backImageView.frame.size.width, _s1mpl3HeadView.backImageView.frame.size.height);
//        [_s1mpl3HeadView removeFromSuperview];
        [self.view addSubview:_imageView];
    }else{
        [_imageView removeFromSuperview];
        _tableView.tableHeaderView = _s1mpl3HeadView;
    }
}


#pragma mark--tableView代理方法


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40 * FITHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    S1mpl3TableViewHeadView *headView = [[S1mpl3TableViewHeadView alloc] initWithFrame:CGRectMake(0, 0, 375 * FITWidth, 40 * FITHeight)];
    headView.totalLabel.text = [NSString stringWithFormat:@"共%@集", _totalString];
    return headView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    S1mpl3AlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[S1mpl3AlbumTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    cell.dic = [_dataSourceArray objectAtIndex:indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130 * FITHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSourceArray.count;
}

/*
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_dataSourceArray.count == 0) {
        return;
    }
    S1mpl3AlbumViewController *albumVC = [[S1mpl3AlbumViewController alloc] init];
    albumVC.ID = [[_dataSourceArray objectAtIndex:indexPath.row] albumId];
    [self.navigationController pushViewController:albumVC animated:YES];
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
